package com.pureut.storage.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.storage.domain.PickingRestDetail;
import com.pureut.storage.dto.PickingRestDetailDto;
import com.pureut.storage.mapper.PickingRestDetailMapper;
import com.pureut.storage.service.PickingRestDetailService;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.dict.utils.DictUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysDictData;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: YSL
 * @Date: 2023/1/19 17:34
 * @Desc: 其他出库 实现层
 */
@Service
public class PickingRestDetailServiceImpl extends ServiceImpl<PickingRestDetailMapper, PickingRestDetail> implements PickingRestDetailService {

    /**
     * 其他出库明细 mapper
     */
    @Resource
    PickingRestDetailMapper pickingRestDetailMapper;

    @DubboReference
    RemoteUnitService remoteUnitService;


    /**
     * 查询其他出库明细列表
     *
     * @param docNum
     * @return
     */
    @Override
    public TableDataInfo<PickingRestDetailDto> getRestList(String docNum, PageQuery pageQuery) {

        // 其他出库明细物料状态

        List<SysDictData> statusArray = CacheUtils.get(CacheNames.SYS_DICT, "picking_rest_detail_status");
        Map<String, String> materialMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        // 明细List
        Page<PickingRestDetailDto> restList = pickingRestDetailMapper.getRestList(pageQuery.build(),docNum);
        for (PickingRestDetailDto pickingRestDetailDto : restList.getRecords()) {
            // 物料状态
            pickingRestDetailDto.setMaterialStatusDict(materialMap.get(String.valueOf(pickingRestDetailDto.getMaterialStatus())));

            String unitName = remoteUnitService.getUnitDataByCode(pickingRestDetailDto.getMaterialUnit()).getData().getUnitName();

            pickingRestDetailDto.setMaterialUnit(pickingRestDetailDto.getMaterialUnit());
            pickingRestDetailDto.setMaterialUnitDict(unitName);

        }


        return TableDataInfo.build(restList);
    }
}
